package com.mad.bikepony.portlet;

import java.text.DateFormat;
import java.util.Date;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.ProcessAction;

import com.liferay.mail.service.MailServiceUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.mail.MailMessage;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.TimeZoneUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.mad.bikepony.model.Delivery;
import com.mad.bikepony.service.DeliveryLocalServiceUtil;

/**
 * Portlet implementation class BikePonyDeliveryRequest
 */
public class BikePonyPortlet extends MVCPortlet {
 

	@ProcessAction(name="addNewParcel")
	public void addNewParcel(ActionRequest request,ActionResponse response)
	{
		String senderEmail = ParamUtil.getString(request, "sender_email", null);
		String senderPickupAddress = ParamUtil.getString(request, "sender_pickupAddress", null);
		String senderName = ParamUtil.getString(request, "sender_name", null);
		String senderSurname = ParamUtil.getString(request, "sender_surname", null);
		String senderAdditionalInfo = ParamUtil.getString(request, "sender_additional_info", null);
		String recipientDeliveryAddress = ParamUtil.getString(request, "recipient_deliveryAddress", null);
		String recipientName = ParamUtil.getString(request, "recipient_name", null);
		String recipientSurname = ParamUtil.getString(request, "recipient_surname", null);
		String recipientAdditionalInfo = ParamUtil.getString(request, "recipient_additional_info", null);
		String recipientEmail = ParamUtil.getString(request, "recipient_email",null);
		response.setRenderParameter("page", "request");
		
		/*
		 * Call the service responsible of creating a new Delivery , even with email addresses
		 */
		try 
		{
			Delivery del = DeliveryLocalServiceUtil.addNewParcel(senderName,senderSurname, senderPickupAddress, senderEmail, senderAdditionalInfo, recipientName, recipientSurname, recipientDeliveryAddress, recipientEmail, recipientAdditionalInfo);
			
			//TODO check the email sending
			
			
			DateFormat df = DateFormatFactoryUtil.getDateTime(LocaleUtil.getDefault(), TimeZoneUtil.getDefault());
			
			/*prepare the email to be sent to the sender*/

			InternetAddress fromAddress = new InternetAddress("admin@bikepony.it");
			
			StringBuilder emailBody = new StringBuilder();
			emailBody.append("Dear sig. <b>")
			.append(senderSurname)
			.append(" "+senderName)
			.append("</b>,<br/>your parcel request has been taken into account.<br/>")
			.append("You can follow the status of your parcel , even the estimated time by entering the following code in the dedicated \"Tracking area \" in our <a href=\"http://localhost:8080/web/guest/tracking\">website<a/><br/>")
			.append("<br/><h3 style=\"color:green\">")
			.append(del.getTrackingCode())
			.append("</h3>")
			.append("<br/>Here the summary of your request:")
			.append("<br/>estimated pickup time: "+df.format(new Date(del.getEstimatedPickupTime())))
			.append("<br/>estimated delivery time: "+df.format(new Date(del.getEstimatedDeliveryTime())))
			.append("<br/><br/>Thank you to have chosen our service.<br/>Best regards, the direction of BikePony.");
			

			MailMessage mm = new MailMessage();
			mm.setFrom(fromAddress);
			mm.setTo(new InternetAddress(del.getSenderEmail()));
			mm.setBody(emailBody.toString());
			mm.setHTMLFormat(true);
			mm.setSubject("Bike Pony service");
			MailServiceUtil.sendEmail(mm);
			
			/*prepare the email to be sent to the recipient*/
			StringBuilder emailBody2 = new StringBuilder();
			emailBody2.append("Dear sig. <b>")
			.append(recipientSurname)
			.append(" "+recipientName)
			.append("</b>,<br/>a parcel has to be sent to you.<br/>")
			.append("When one of our agents will reach your address, you will be asked to give the following delivery-code in order to complete the delivery<br/>")
			.append("<br/><h3 style=\"color:green\">")
			.append(del.getDeliveryCode())
			.append("</h3>")
			.append("<br/>The estimated delivery time is : "+df.format(new Date(del.getEstimatedDeliveryTime())))
			.append("<br/><br/>Thank you to have chosen our service.<br/><br/>Best regards, the direction of BikePony.");
			
			MailMessage mm1 = new MailMessage();
			mm1.setFrom(fromAddress);
			mm1.setTo(new InternetAddress(del.getRecipientEmail()));
			mm1.setBody(emailBody2.toString());
			mm1.setHTMLFormat(true);
			mm1.setSubject("Bike Pony service");			
			MailServiceUtil.sendEmail(mm1);
			
			request.setAttribute("delivery", del);
		} catch (SystemException e) {
			SessionErrors.add(request, "error_new_parcel_request",e.getMessage());
			e.printStackTrace();
		} catch (AddressException e) {
			e.printStackTrace();
		}
		
	}
}
